package com.cimstech.connect;

import com.cimstech.model.Value;
import monfox.stack.iec61850.api.DATYPE;
import monfox.stack.iec61850.api.ModelException;
import monfox.stack.iec61850.api.ModelValueException;
import monfox.stack.iec61850.api.client.IEC61850ClientApi;
import monfox.stack.iec61850.api.types.DataValue;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.ws.rs.ext.Provider;

public class Commands {
    /**
     * 通用链接api
     */
    private static IEC61850ClientApi api;
    public static final String ALLMODEL = "all_model";
    public static final String LDS = "lds";
    public static final String LNS = "lns";
    public static final String FCS = "fcs";
    public static final String DOS = "dos";
    public static final String DAS = "das";
    public static final String DSS = "dss";
    public static final String BRS = "brs";
    public static final String RPS = "rps";
    public static final String LGS = "lgs";
    public static final String GI = "gi";
    public static final String FCDAS = "fcdas";
    public static final String READ = "read";
    public static final String ENABLE = "enable";
    public static final String ENABLE_URCB = "enable_urcb";
    public static final String ENABLE_BRCB = "enable_brcb";
    public static final String DISABLE_URCB = "disable_urcb";
    public static final String DISABLE_BRCB = "disable_brcb";
    public static final String HISDATA = "hisdata";
    public static final String REPORT_LOG = "report_log";
    public static final String LIST_FILES = "list_files";
    public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static Commands instance = new Commands();

    private Commands() {
        try {
            System.out.println("类实例化，创建api对象");
            Commands.api = IEC61850ClientApi.newInstance("mms");
        } catch (ModelException e) {
            e.printStackTrace();
        }
    }

    public static Commands getInstance() {
        return instance;
    }

    public static IEC61850ClientApi getApi() throws ModelException {
        if (api == null) {
            System.out.println("创建新api对象");
            return IEC61850ClientApi.newInstance("mms");
        } else {
            System.out.println("返回已有api对象");
            return api;
        }
    }

    /**
     * 将一个structure类型的DataValue对象转合成约定的量测值三元组
     *
     * @param data
     * @return
     * @throws ModelValueException
     */
    public static Value dataValueString(DataValue data, String key) throws ModelValueException {
        if (!data.isStructure()) {
            return new Value();
        }
        List<DataValue> elements = data.getChildren();
        if (elements.size() != 3 && elements.size() != 4) {
            return new Value();
        }
        String v = "", q = "", t = "";
        for (int i = 0; i < elements.size(); i++) {
            DataValue dataValue = elements.get(i);
            if (dataValue.isArray()) {
                continue;
            }
            if (dataValue.getType().getTypeId() == DATYPE.TIMESTAMP) {
                t = df.format(elements.get(i++).getTimestamp().getDateTime());
            } else if (dataValue.getTypeId() == DATYPE.CODED_ENUM) {
                String $q = dataValue.getBits().toString().replace("[", "").replace("]", "");
                q = getQStr($q);
            } else {
                if (dataValue.getTypeId() == DATYPE.BOOLEAN) {
                    if (dataValue.booleanValue()) {
                        v = "1";
                    } else {
                        v = "0";
                    }
                } else if (dataValue.getTypeId() == DATYPE.INTEGER) {
                    v = dataValue.getRawString();
                } else if (dataValue.getTypeId() == DATYPE.FLOAT) {
                    v = dataValue.getRawString();
                } else if (dataValue.isStructure()) {
                    for (DataValue val : dataValue.getChildren()) {
                        if (val.getTypeId() == DATYPE.FLOAT) {
                            v = val.getRawString();
                        }
                    }
                }
            }
        }
        return new Value(key, q, t, v);
    }

    public static String getQStr(String aValue) {
        String result = "";
        if (aValue.length() == 13) {
            String str = aValue.substring(0, 2);
            if (str.equals("00"))
                result = result + "正常、";
            else if (str.equals("01"))
                result = result + "无效、";
            else if (str.equals("10"))
                result = result + "保留、";
            else if (str.equals("11"))
                result = result + "可疑、";
            str = aValue.substring(2, 3);
            if (str.equals("1"))
                result = result + "溢出、";
            str = aValue.substring(3, 4);
            if (str.equals("1"))
                result = result + "超值域、";
            str = aValue.substring(4, 5);
            if (str.equals("1"))
                result = result + "坏基准值、";
            str = aValue.substring(5, 6);
            if (str.equals("1"))
                result = result + "抖动、";
            str = aValue.substring(6, 7);
            if (str.equals("1"))
                result = result + "故障、";
            str = aValue.substring(7, 8);
            if (str.equals("1"))
                result = result + "旧数据、";
            str = aValue.substring(8, 9);
            if (str.equals("1"))
                result = result + "不一致、";
            str = aValue.substring(9, 10);
            if (str.equals("1"))
                result = result + "不准确、";
            str = aValue.substring(10, 11);
            if (str.equals("1"))
                result = result + "源取代、";
            str = aValue.substring(11, 12);
            if (str.equals("1"))
                result = result + "测试、";
            str = aValue.substring(12, 13);
            if (str.equals("1"))
                result = result + "操作员闭锁、";
            if (result.endsWith("、")) {
                result = result.substring(0, result.length() - 1);
                result = result + "(" + aValue + ")";
            }
        } else
            result = aValue;
        return result;
    }
}
